package com.matt.controller.web;

import com.dotflat.util.excelUtil.ExcelDataVo;
import com.dotflat.util.excelUtil.PackExcelSheetsDataUtil;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.ChildrenExtendInfo;
import com.matt.domain.ChildrenNurseryInfoRecord;
import com.matt.foundation.constant.RequestConstant;
import com.matt.foundation.ext.aliyun.OSSProcessService;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.utils.DateUtil;
import com.matt.service.NurseryInfoRecordService;
import io.swagger.annotations.*;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/7/19
 * *******************************
 */
@RestController
@RequestMapping("/web/nurseryInfoRecord")
@Api(tags = "考勤流水模块")
public class WebNurseryInfoRecordController {

    @Autowired
    private NurseryInfoRecordService nurseryInfoRecordService;

    @Autowired
    private OSSProcessService ossProcessService;

    @Autowired
    private MemVerificationCache cache;

    @Value("${aliyun.oss.fileServerPrefix}")
    private String IMG_SERVICE_PREFIX;

//    @ApiOperation(value = "添加园区合同日期 ")
//    @RequestMapping(value = "/updateContractDate" ,method = RequestMethod.POST)
//    @ResponseBody
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
//            @ApiImplicitParam(paramType = "query", name = "day", value = "天数", required = true, dataType = "int"),    })
//    public StatusResponse updateContractDate(@RequestHeader(RequestConstant.TICKET) String ticket,Integer day) {
//        StaffDTO staffDTO  = cache.getStaff(ticket);
//        nurseryInfoRecordService.updateContractDate(staffDTO,day);
//        return StatusResponse.success("添加成功！");
//    }

    @ApiOperation(value = "添加指定学生合同日期")
    @RequestMapping(value = "/updateChildrenIdContractDate" ,method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "childrenId", value = "学生ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "day", value = "天数", required = true, dataType = "int"),
    })
    @ApiResponses({
            @ApiResponse(code = 000000, message = "successful", response = ChildrenExtendInfo.class),
    })
    public StatusResponse updateChildrenIdContractDate(@RequestHeader(RequestConstant.TICKET) String ticket,String childrenId,Integer day) {
        StaffDTO staffDTO  = cache.getStaff(ticket);
        ChildrenExtendInfo childrenExtendInfo = nurseryInfoRecordService.updateChildrenIdContractDate(ticket,staffDTO,childrenId,day);
        return StatusResponse.success(childrenExtendInfo);
    }


//    @ApiOperation(value = "查询列表")
    @RequestMapping(value = "/list" ,method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "childrenId", value = "学生ID", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "beginDate", value = "开始时间", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "endDate", value = "结束时间", required = false, dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code = 000000, message = "successful", response = ChildrenNurseryInfoRecord.class),
    })
    public StatusResponse list(@RequestHeader(RequestConstant.TICKET) String ticket, String childrenId, String beginDate, String endDate, PagerQuery pagerQuery) {
        Page<ChildrenNurseryInfoRecord> page = nurseryInfoRecordService.getList(childrenId,beginDate,endDate,pagerQuery);
        return StatusResponse.success(page);
    }


    @ApiOperation(value = "查询列表")
    @RequestMapping(value = "/listByAreaId" ,method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "childrenName", value = "学生名称", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "beginDate", value = "开始时间", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "endDate", value = "结束时间", required = false, dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code = 000000, message = "successful", response = ChildrenNurseryInfoRecord.class),
    })
    public StatusResponse listByAreaId(@RequestHeader(RequestConstant.TICKET) String ticket, String childrenName, String beginDate, String endDate, PagerQuery pagerQuery) {
        StaffDTO staff = cache.getStaff(ticket);
        String areaId = staff.getAreaId();
        Page<ChildrenNurseryInfoRecord> page = nurseryInfoRecordService.getListByAreaId(areaId,childrenName,beginDate,endDate,pagerQuery);
        return StatusResponse.success(page);
    }

//    @ApiOperation(value = "获取最新工作日 - 保留接口")
//    @RequestMapping(value = "/getWorkDate" ,method = RequestMethod.POST)
//    @ResponseBody
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
//    })
//    public StatusResponse getWorkDate(@RequestHeader(RequestConstant.TICKET) String ticket) {
//        nurseryInfoRecordService. getWorkDate();
//        return StatusResponse.success();
//    }


    @ApiOperation(value = "获取课时信息")
    @RequestMapping(value = "/getBalance" ,method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "childrenId", value = "学生ID", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "beginDate", value = "开始时间", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "endDate", value = "结束时间", required = false, dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code = 000000, message = "successful", response = ChildrenNurseryInfoRecord.class),
    })
    public StatusResponse getBalance(@RequestHeader(RequestConstant.TICKET) String ticket, String childrenId, String beginDate, String endDate, PagerQuery pagerQuery) {
        StaffDTO staffDTO  = cache.getStaff(ticket);
        Map  m = nurseryInfoRecordService.getBalance(staffDTO,childrenId,beginDate,endDate,pagerQuery);
        return StatusResponse.success(m);
    }



    @ApiOperation(value = "月统计信息")
    @RequestMapping(value = "/getMonthInfo" ,method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "beginDate", value = "开始时间", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "endDate", value = "结束时间", required = true, dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code = 000000, message = "successful", response = ChildrenNurseryInfoRecord.class),
    })
    public StatusResponse getMonthInfo(@RequestHeader(RequestConstant.TICKET) String ticket,
                                       String classId,String name, String beginDate, String endDate) {
        StaffDTO staffDTO  = cache.getStaff(ticket);
        List<Map<String, Object>> m = nurseryInfoRecordService.getMonthInfo(staffDTO,classId,name,beginDate,endDate);
        return StatusResponse.success(m);
    }



    @ApiOperation(value = "月统计信息 - 导出")
    @RequestMapping(value = "/exportMonthInfoSheet" ,method = RequestMethod.POST)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "beginDate", value = "开始时间", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "endDate", value = "结束时间", required = true, dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code = 000000, message = "successful", response = ChildrenNurseryInfoRecord.class),
    })
    public StatusResponse exportMonthInfoSheet(HttpServletResponse response, @RequestHeader(RequestConstant.TICKET) String ticket, String classId, String name, String beginDate, String endDate) {
        StaffDTO staffDTO  = cache.getStaff(ticket);
        List<Map<String, Object>> m = nurseryInfoRecordService.exportMonthInfoSheet(staffDTO, classId,name,beginDate,endDate);

        // 封装需要导出的数据
        List<ExcelDataVo> excelDataVoList = new ArrayList<>();

        //所有天
        List<String> dateList = DateUtil.getDay(beginDate,endDate);

        List<String> dateList1 = new ArrayList<>();
        dateList1.add("学生名字");
        dateList1.add("当月出勤");
        dateList1.add("当月事假");
        dateList1.add("当月病假");

        dateList1.add("当月应出勤");
        dateList1.add("出勤率");
        dateList1.add("月消课总额");
        dateList1.add("月合同总额");

        dateList1.add("合同期内消课数");
        dateList1.add("合同总额");
        dateList1.add("预计合同总出勤数");
        dateList1.add("合同剩余可延课");
        dateList1.addAll(dateList);


        List<String> dateList2 = new ArrayList<>();
        dateList2.add("name");
        dateList2.add("attendanceCount");
        dateList2.add("leaveCount");
        dateList2.add("sickCount");

        dateList2.add("shouldAttendanceCount");
        dateList2.add("attendanceProbability");
        dateList2.add("practicalAmount");
        dateList2.add("totalAmount");

        dateList2.add("contractFullCount"); //dateList1.add("合同期内消课数");
        dateList2.add("contractTotalAmount"); //dateList1.add("合同总额");
        dateList2.add("contractPreferCount"); //dateList1.add("预计合同总出勤数");
        dateList2.add("surplusLeaveDay"); //dateList1.add("合同剩余可延课");
        dateList2.addAll(dateList);

        // 封装学生信息
        ExcelDataVo excelDataSheetOne = new ExcelDataVo();
        excelDataSheetOne.setSheetName("班级学生考勤信息");
        excelDataSheetOne.setSheetTopColHeaderTitle("班级学生考勤信息");
        excelDataSheetOne.setSheetTopColHeaderName(dateList1.toArray(new String[dateList1.size()]));
        excelDataSheetOne.setSheetTopColHeaderAttribute(dateList2.toArray(new String[dateList2.size()]));
        excelDataSheetOne.setSheetDataList(m);
        excelDataVoList.add(excelDataSheetOne);

        String url= "";
        // 导出数据
        try {
            url  = new PackExcelSheetsDataUtil().packExcelSheetsData(response, "学生基础信息", excelDataVoList);
        } catch (Exception e) {
            throw new Exception("导出异常!");
        }finally {
            File outputFile = new File(url);
            Calendar cal = Calendar.getInstance();
            cal.setTime(DateUtil.parseToDateMilisV3(beginDate));
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH) + 1;
            String uploadPath = "teach/file/children_export_excel/" + DateTime.now().toString("yyyy-MM") + "/"+year+"年"+month+"月"+"考勤信息报表.xls";
            String relativePath = ossProcessService.aliyunOssUpload(uploadPath, outputFile);
            String uploadedResult = IMG_SERVICE_PREFIX + relativePath;
            return StatusResponse.success(uploadedResult);
        }
    }
}
